2;
/**
 * create by young (young2f@outlook.com) on 2024/4/21
 */
'use strict';
const base = require('../base/controllerBase');
const _stringUtils = require('../utils/stringUtils');
const { code } = require('../base/modelBase');
const _ = require('lodash');
const table = 'ExperimentStore';

class ExperimentController extends base {
  async createStore() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        name: { type: 'string' },
      };
      console.log('ccc', body);
      ctx.validate(model, body);
      const { code } = body;
      body.code = code ? code : _stringUtils.getCode('EXS');
      const result = await ctx.app.dbs.insert(table, body);
      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async deleteStore() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        code: { type: 'string' },
      };

      ctx.validate(model, body);
      const { code, tenantCode } = body;
      const result = await ctx.app.dbs.deleteOne(table, { code, tenantCode });
      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async updateStore() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        code: { type: 'string' },
      };

      ctx.validate(model, body);
      const { code, tenantCode } = body;
      const result = await ctx.app.dbs.updateOne(table, { code, tenantCode }, body);
      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async findStoreMany() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
      };

      ctx.validate(model, body);
      const { code, tenantCode, name } = body;
      const query = { tenantCode };
      if (name) query.name = name;
      if (code) query.code = code;
      const result = await ctx.app.dbs.find(table, query);
      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async createRecord() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
      };
      ctx.validate(model, body);
      const { code } = body;
      body.code = code ? code : _stringUtils.getCode('EXR');
      const result = await ctx.app.dbs.insert('ExperimentRecord', body);
      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async deleteRecord() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        code: { type: 'string' },
      };
      ctx.validate(model, body);
      const { code, tenantCode } = body;
      const result = await ctx.app.dbs.deleteOne('ExperimentRecord', { code, tenantCode });
      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async updateRecord() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        code: { type: 'string' },
      };
      ctx.validate(model, body);
      const { code, tenantCode } = body;
      const result = await ctx.app.dbs.updateOne('ExperimentRecord', { code, tenantCode }, body);
      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async findRecordMany() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
      };
      ctx.validate(model, body);
      const { code, tenantCode ,experimentType,customer,salesmen,experimentMan,experimentName} = body;
      const query = { tenantCode };
      if(code) query.code =  {$regex:code};
      if(experimentType) query.experimentType =  {$regex:experimentType};
      if(customer) query.customer ={$regex:customer};
      if(salesmen) query.salesmen = {$regex:salesmen};
      if(experimentMan) query.experimentMan = {$regex:experimentMan};
      if(experimentName) query.experimentName = {$regex:experimentName};
      const result = await ctx.app.dbs.find('ExperimentRecord', query);
      console.log('find experimentRecord', result);
      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }
}

module.exports = ExperimentController;
